<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属班级" prop="classId">
        <el-input
          v-model="queryParams.classId"
          placeholder="请输入所属班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="老师" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入老师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择学生状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_student_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模拟面试" prop="mockInterview">
        <el-select v-model="queryParams.mockInterview" placeholder="请选择模拟面试" clearable>
          <el-option
            v-for="dict in dict.type.sys_simulation_interview"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="结业测试" prop="finalTest">
        <el-select v-model="queryParams.finalTest" placeholder="请选择结业测试" clearable>
          <el-option
            v-for="dict in dict.type.sys_final_test"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="困难系数" prop="difficulty">
        <el-select v-model="queryParams.difficulty" placeholder="请选择困难系数" clearable>
          <el-option
            v-for="dict in dict.type.sys_difficult_coefficient"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['eduadmin:attention:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['eduadmin:attention:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['eduadmin:attention:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['eduadmin:attention:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!--列表展示-->
    <el-table v-loading="loading" :data="attentionList"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="所属班级" align="center" prop="className" />
      <el-table-column label="老师" align="center" prop="teacherName" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="学生状态" align="center" prop="state">
        <template slot-scope="scope">
          <el-select v-model="scope.row.state" :value="form.state" id="updateForm" @change="handleAttentionUpdate(scope.row)">
            <el-option
              v-for="dict in dict.type.sys_student_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>

      <el-table-column label="模拟面试" align="center" prop="mockInterview">
        <template slot-scope="scope">
          <el-select v-model="scope.row.mockInterview" :value="form.mockInterview" id="updateForm" @change="handleAttentionUpdate(scope.row)">
            <el-option
              v-for="dict in dict.type.sys_simulation_interview"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>


      <el-table-column label="结业测试" align="center" prop="finalTest">
        <template slot-scope="scope">
          <el-select v-model="scope.row.finalTest" :value="form.finalTest" id="updateForm" @change="handleAttentionUpdate(scope.row)">
            <el-option
              v-for="dict in dict.type.sys_final_test"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>

      <el-table-column label="困难系数" align="center" prop="difficulty">
        <template slot-scope="scope">
          <el-select v-model="scope.row.difficulty" :value="form.difficulty" id="updateForm" @change="handleAttentionUpdate(scope.row)">
            <el-option
              v-for="dict in dict.type.sys_difficult_coefficient"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleTrace(scope.row)"
            v-hasPermi="['eduadmin:attention:trace']"
          >跟踪记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['eduadmin:attention:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['eduadmin:attention:remove']"
          >不再关注</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改关注列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="老师" prop="teacherName">
          <el-select v-model="form.teacherName" placeholder="请输入老师">
            <el-option
              v-for="item in teachers"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属班级" prop="className">
          <el-select v-model="form.className" @change="classChange" placeholder="请输入所属班级">
            <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学生姓名" prop="studentName">
          <el-select v-model="form.studentName" placeholder="请输入学生姓名">
            <el-option
              v-for="(item,index) in studentNameList"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择学生状态">
            <el-option
              v-for="dict in dict.type.sys_student_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模拟面试" prop="mockInterview">
          <el-select v-model="form.mockInterview" placeholder="请选择模拟面试">
            <el-option
              v-for="dict in dict.type.sys_simulation_interview"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结业测试" prop="finalTest">
          <el-select v-model="form.finalTest" placeholder="请选择结业测试">
            <el-option
              v-for="dict in dict.type.sys_final_test"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="困难系数" prop="difficulty">
          <el-select v-model="form.difficulty" placeholder="请选择困难系数">
            <el-option
              v-for="dict in dict.type.sys_difficult_coefficient"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="留级状态" prop="socre" >
          <el-switch v-model="form.addrepetition" >
          </el-switch>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 追踪记录表单 -->
    <el-dialog :title="title" :visible.sync="traceOpen" width="500px" append-to-body>
      <el-form ref="traceForm" :model="traceForm" :rules="rules" label-width="80px">
        <el-timeline>
          <el-timeline-item v-for="o in traceForm" :key="o" :timestamp="o.happenTime" placement="top">
            <el-card class="box-card"  style="width: 360px">
              <h4>跟踪人[{{ o.traceTeacher }}]</h4>
              <p>{{"问题说明:" + o.remark}}</p>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listAttention, getAttention, delAttention, addAttention, updateAttention ,traceAttention} from "@/api/eduadmin/attention";
import {listClass} from "@/api/student/class";
import { listTeacherManagement} from "@/api/teacher/teacherManagement";
import { addRepetition} from "@/api/eduadmin/repetition";
import {getStudentByClassId} from "@/api/student/studentMessage";

export default {
  name: "Attention",
  dicts: ['sys_difficult_coefficient', 'sys_student_state', 'sys_test_state','sys_simulation_interview','sys_final_test'],
  data() {
    return {
      value : false,
      classList:[],
      students:[],
      teachers:[],
      studentNameList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 关注列表表格数据
      attentionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      traceOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className: null,
        teacherName: null,
        studentName: null,
        state: null,
        mockInterview: null,
        finalTest: null,
        difficulty: null
      },
      // 表单参数
      form: {},
      traceForm: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.findClassList();
    this.getTeachers();
    this.getStudents();
  },
  methods: {
    // 修改atttention数据字典
    handleAttentionUpdate(row) {
      updateAttention(row).then(response=>{
      })
    },

    // 加入留级方法
    joinRepetition(){
      addRepetition(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },

    /** 根据班级id查询所有学生 */
    classChange(value) {
      getStudentByClassId().then(response => {
        this.test = response;
        for (let i in this.test) {
          if (this.test[i].name == value) {
            this.studentNameList = this.test[i].names;
          }
        }
        console.log(this.studentNameList);
      });
    },


    /** 查询所有老师 */
    getTeachers() {
      listTeacherManagement().then(response => {
        this.teachers = response.rows;
        this.total = response.total;
      });
    },
    /** 查询关注列表列表 */
    getList() {
      this.loading = true;
      listAttention(this.queryParams).then(response => {
        this.attentionList = response.rows;
        console.log(this.attentionList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.traceOpen = false;
      this.reset();
    },
    // canceltrace(){
    //   this.traceOpen = false;
    //   this.resetTrace();
    // },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        className: null,
        teacherName: null,
        studentName: null,
        state: null,
        mockInterview: null,
        finalTest: null,
        difficulty: null,
        studentId: null,
        // addrepetition: null,
      };
      this.resetForm("form");
    },

    // 跟踪表单重置
    resetTrace() {
      this.traceForm = {
        studentId: null,
        id: null,
        happenTime: null,
        studentName: null,
        traceTeacher: null,
        remark: null,
      };
      this.resetForm("traceForm");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加关注列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAttention(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改关注列表";
      });
    },

    // 跟踪记录按钮操作
    handleTrace(row) {
      this.resetTrace();
      const id = row.id || this.ids
      traceAttention(row.studentId).then(response => {
        console.log(this.form.studentId);
        this.traceForm = response;
        console.log(this.traceForm);
        this.traceOpen = true;
        this.title = this.traceForm[0].studentName + "的跟踪记录";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (this.form.addrepetition != null) {
          addRepetition(this.form).then(response => {
            this.$modal.msgSuccess("添加留级成功");
            this.open = false;
            this.getList();
          });
        }
        if (valid) {
          if (this.form.id != null) {
            updateAttention(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttention(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认取消对列表编号为"' + ids + '"的关注？').then(function() {
        return delAttention(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('eduadmin/attention/export', {
        ...this.queryParams
      }, `attention_${new Date().getTime()}.xlsx`)
    },

    /** 查询班级管理列表 */
    findClassList() {
      this.loading = true;
      listClass(this.queryParams).then(response => {
        this.classList = response.rows;
        // console.log(this.classList)
      });
    },
  }
};
</script>

<style>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.box-card {
  width: 480px;
}
</style>
